<?php

namespace app\api\model;

use think\Model;

class Leave extends Model
{
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function getCreateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s') : '';
    }

    public function getUpdateTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s') : '';
    }

    public static function getLeavesByParent($parent_id)
    {
        return self::with('teacher')->where('parent_id', $parent_id)->select();
    }

    public static function getLeavesByTeacher($teacher_id, $length = 15)
    {
        return self::with('parent')->where('teacher_id', $teacher_id)->paginate($length);
    }

    public function parent()
    {
        return $this->hasOne(Parents::class, 'id', 'parent_id');
    }

    public function teacher()
    {
        return $this->hasOne(Teachers::class, 'id', 'teacher_id')->field(['id', 'username']);
    }
}

